package com.yxq.mdt.app.modules.withdraw.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.bean.RechargeListBean;
import com.yxq.mdt.app.modules.personal.bean.RechargePayResultBean;
import com.yxq.mdt.app.modules.personal.bean.RechargeWxPayResultBean;
import com.yxq.mdt.app.modules.personal.model.RechargeModel;
import com.yxq.mdt.app.modules.personal.presenter.IRechargeContract;
import com.yxq.mdt.app.modules.personal.presenter.RechargePresenter;
import com.yxq.mdt.app.modules.withdraw.bean.WithDrawResultBean;
import com.yxq.mdt.app.net.API;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 提现
 */
public class WithDrawActivity extends BaseActivity<RechargePresenter> implements IRechargeContract.View {


    @BindView(R.id.draw_tv_balance)
    TextView drawTvBalance;
    @BindView(R.id.draw_tv_account)
    EditText drawTvAccount;
    @BindView(R.id.draw_tv_update)
    TextView drawTvUpdate;
    @BindView(R.id.draw_et_before_money)
    EditText drawEtBeforeMoney;
    @BindView(R.id.draw_tv_after_money)
    TextView drawTvAfterMoney;
    @BindView(R.id.draw_btn_submit)
    Button drawBtnSubmit;
    @BindView(R.id.draw_checkbox)
    CheckBox drawCheckbox;
    @BindView(R.id.draw_tv_doc)
    TextView drawTvDoc;
    float honeyDrill = 0f;//蜜钻
    @BindView(R.id.login_et_name)
    EditText loginEtName;
    @BindView(R.id.draw_tv_contact_phone)
    EditText drawTvContactPhone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_with_draw;
    }

    @Override
    public void initView() {
        MyPersonalBean bean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
        honeyDrill = bean.getHoneyDrill();
        drawTvBalance.setText(String.format("%.2f", bean.getHoneyDrill()));
        drawEtBeforeMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (drawEtBeforeMoney.getText().length() > 0) {

                    if (Double.parseDouble(drawEtBeforeMoney.getText().toString()) >= 100d) {
                        double p = Double.parseDouble(drawEtBeforeMoney.getText().toString());
                        if (p > honeyDrill) {
                            ToastUtils.showShort("提现数目不能大于余额");
                            drawEtBeforeMoney.setText(honeyDrill + "");
                            drawEtBeforeMoney.setSelection((honeyDrill + "").length());
                            drawTvAfterMoney.setText(String.format("%.2f", honeyDrill / 100f));
                            setButtonEnable();
                            return;
                        }
//                        if (!drawCheckbox.isChecked()) {
//                            ToastUtils.showShort("请先同意《用户兑换协议》");
//                        }
                        drawTvAfterMoney.setText(String.format("%.2f", p / 100f));
                        setButtonEnable();
                    } else {
                        ToastUtils.showShort("最低100蜜钻起提");
                        drawTvAfterMoney.setText("0.0");
                        setButtonEnable();
                    }
                } else {
                    drawTvAfterMoney.setText("0.0");
                    setButtonEnable();
                }
            }
        });
        drawCheckbox.setOnCheckedChangeListener((compoundButton, b) -> setButtonEnable());
    }

    /**
     * 控制按钮能否点击
     */
    private void setButtonEnable() {
        if (drawTvAfterMoney.getText().toString().length() != 0) {
            double p = Double.parseDouble(drawTvAfterMoney.getText().toString());
            if (drawCheckbox.isChecked() && p > 0d) {
                drawBtnSubmit.setEnabled(true);
            } else {
                drawBtnSubmit.setEnabled(false);
            }
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new RechargePresenter(this, new RechargeModel());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == 10086) {
                //账号
                String account = data.getStringExtra("account");
                drawTvAccount.setText(account);
            } else if (requestCode == 10010) {
                //校验手机号码通过,发起提现操作

            }
        }
    }

    @OnClick({R.id.draw_tv_update, R.id.draw_btn_submit, R.id.draw_tv_doc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.draw_tv_update:
                startActivityForResult(new Intent(mActivity, BindAliPayActivity.class), 10086);
                break;
            case R.id.draw_btn_submit:
                if (honeyDrill == 0) {
                    drawBtnSubmit.setEnabled(true);
                }
                submit();
                break;
            case R.id.draw_tv_doc:
                Intent intent = new Intent(mActivity, MiWebViewActivity.class);
                intent.putExtra("url", API.USER_AGREEMENT);
                startActivity(intent);
                break;
        }
    }

    /**
     * 提现
     */
    private void submit() {
        if (drawTvAccount.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入支付宝账号");
            return;
        }
        if (loginEtName.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        if (drawEtBeforeMoney.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入提现金额");
            return;
        }
        if (Double.parseDouble(drawEtBeforeMoney.getText().toString()) < 100d) {
            ToastUtils.showShort("最低100蜜钻起提");
            return;
        }
        if (drawTvContactPhone.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入联系电话");
            return;
        }

        showLoadingDialog();
        mPresenter.cashdrill(drawEtBeforeMoney.getText().toString(), 1, 1, drawTvAccount.getText().toString(), loginEtName.getText().toString(), null, null, drawTvContactPhone.getText().toString());
    }

    @Override
    public void onLoadSuccess(RechargeListBean data) {

    }

    @Override
    public void onLoadFailed(String msg) {

    }

    @Override
    public void onRechargeSuccess(RechargePayResultBean data) {

    }

    @Override
    public void onRechargeFailed(String msg) {

    }

    @Override
    public void onRechargeWxSuccess(RechargeWxPayResultBean data) {

    }

    @Override
    public void onRechargeWxFailed(String msg) {

    }

    @Override
    public void onCashdrillSuccess(WithDrawResultBean data) {
        hideLoadingDialog();
        if (data != null) {
            ToastUtils.showShort("申请提现成功");
            if (data.getStatus() == 200) {
                finish();
            }
        }
    }

    @Override
    public void onCashdrillFailed(String msg) {
        hideLoadingDialog();
        ToastUtils.showShort(msg + "");
    }

}
